package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastLoginRecordService;
import com.runfast.paotui.service.RunfastShopperService;


import com.runfast.waimai.util.Constant;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@Controller
@RequestMapping("/driver/install/*")
public class DriverInstallController extends DriverBaseController {
	//修改密码 姓名，手机号码 退出
    @Resource
    private RunfastShopperService shopperService;
	@Resource
	RunfastLoginRecordService runfastLoginRecordService;
	
	@RequestMapping(value="index",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap index(String token){
		ModelMap model = new ModelMap();
		RunfastShopper shopper = super.getLoginShopper(token);
		if(shopper!=null){
			RunfastShopper sh = 	shopperService.get(shopper.getId());
			if(sh!=null){
				model.put("shopper", sh);
			}
		}	
		return model;
 }

	//修改密码
	@RequestMapping(value="updatepassword",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap updatepassword(@RequestParam("password") String password, @RequestParam("newpassword") String newpassword,
												 @RequestParam("aginnewpassword") String aginnewpassword,String token){
		ModelMap model = new ModelMap();
	     if(super.getLoginShopper(token)!=null){
	     RunfastShopper shopper  = shopperService.get(super.getLoginShopper(token).getId());
	      if(shopper!=null){
	    	  if(password!=null&&newpassword!=null&&aginnewpassword!=null){
	    	 if(shopper.getPassword().equals(DigestUtils.md5Hex(password))){
	    		 shopper.setPassword(DigestUtils.md5Hex(newpassword));//设置加密密码
	    		 shopperService.update(shopper);
	    		 model.put("succ", "修改成功");
	    	 }else{
	    		 model.put("flas", "原密码不正确！");
	    	 }
	     }
	   }
  }
	       	return model;
	}
	//修改手机号
	@RequestMapping(value="upmunber",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap upmunber(@RequestParam(value = "munber") String munber,String token){
		ModelMap model = new ModelMap();
     RunfastShopper shopper   = super.getLoginShopper(token);
     if(shopper!=null){
    	 RunfastShopper sho    = shopperService.get(shopper.getId());
    	 if(munber!=null){
    		 sho.setMunber(munber);
    		 //这里可以优化，update 表名 set number=#{munber} where id=#{id}
			 shopperService.updateNumber(sho);
    	//	 shopperService.updateShopper(sho);
    		 model.put("succ","修改成功");
    	 }
     }
		
		return model;
}
	////修改姓名
	@RequestMapping(value="upname",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap upname(@RequestParam(value = "name") String name,String token){
		ModelMap model = new ModelMap();
     RunfastShopper shopper   = super.getLoginShopper(token);
     if(shopper!=null){
    	 RunfastShopper sho    = shopperService.get(shopper.getId());
    	 if(name!=null){
    		 sho.setName(name);
    		 shopperService.update(sho);
    		 model.put("succ","修改成功！");
    	 }
     }
	          return model;
	}

	//退出
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	public  @ResponseBody ModelMap logout(HttpServletRequest request, HttpServletResponse response,String token) {
		    ModelMap model = new ModelMap();

		((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
		.getRequest().getSession().removeAttribute(Constant.APP_DRIVER_SESSION);/*配送员*/
				RunfastShopper runfastLoginRecord=super.getLoginShopper(token);
				//runfastLoginRecord.setLogouttime(new Date());//设置退出时间
			//	runfastLoginRecordService.deleteshopper(super.getLoginShopper(token).getId());
		     model.put("succ","已退出！");

	       	return model;
	}
}
